package com.tubeonfire.controller;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.tubeonfire.entity.Author;
import com.tubeonfire.model.admin.AuthorModel;
import com.tubeonfire.service.YoutubeService;

@SuppressWarnings("serial")
public class TestServlet extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			UserService userService = UserServiceFactory.getUserService();
			User user = userService.getCurrentUser();
			if (user != null && userService.isUserAdmin()) {
				if (req.getParameter("action").equals("add-author")) {
					String userId = req.getParameter("userId");
					String uri = "https://gdata.youtube.com/feeds/api/users/"
							+ userId;
					if (uri != null && uri.length() > 0) {
						if (AuthorModel.getById(uri) == null) {
							Author author = new Author();
							author.setUri(uri);
							String[] spl = uri.split("/");
							author.setName(spl[spl.length - 1]);
							author = YoutubeService.getUserProfile(author);
							author.setStatus(1);
							author.setBumpPoint(Calendar.getInstance()
									.getTimeInMillis() / 1000);
							author.setUpdated(Calendar.getInstance().getTime());
							AuthorModel.insert(author);
							com.tubeonfire.model.AuthorModel.clearModelCache();
						}
					}
				}
			}
		} catch (Exception e) {

		}

	}
}
